package com.zhangtai.modules.service.impl.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.dao.ContractGroupDao;
import com.zhangtai.modules.dao.ContractSubGroupDao;
import com.zhangtai.modules.entity.system.ContractGroupEntity;
import com.zhangtai.modules.entity.system.ContractSubGroupEntity;
import com.zhangtai.modules.service.system.ContractGroupService;
import com.zhangtai.modules.service.system.ContractSubGroupService;
import com.zhangtai.modules.vo.ContractMainSubVO;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import com.zhangtai.utils.SnowflakeIdWorker;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


@RestController
public class ContractGroupServiceImpl extends ServiceImpl<ContractGroupDao, ContractGroupEntity> implements ContractGroupService {

    @Autowired
    private ContractSubGroupService contractSubGroupService;

    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    @Autowired
    private ContractSubGroupDao contractSubGroupDao;
    @Autowired
    private ContractGroupDao contractGroupDao;

    @Override
    public PageResult queryPage(@RequestParam(value = "groupName", required = false) String groupName,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

//        ContractGroupEntity contractGroupEntity = contractGroupDao.selectById(contractGroupId);


        IPage<ContractGroupEntity> page = this.page(
                new Page<ContractGroupEntity>(pageNum,pageSize),
                new QueryWrapper<ContractGroupEntity>().like(!StringUtils.isEmpty(groupName),"group_name",groupName)
                        .eq("is_delete",0)
        );

        return new PageResult(page);
    }

    @Override
    public ContractGroupEntity getByIdE(Long id) {
        ContractGroupEntity one = this.getOne(new QueryWrapper<ContractGroupEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(ContractGroupEntity entity) {
        entity.setIsDelete(0);
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    @Transactional
    public R save(ContractMainSubVO contractMainSubVO) {
        ContractGroupEntity contractGroupEntity = new ContractGroupEntity();
        if(contractMainSubVO.getId()!=null){
            contractGroupEntity.setId(new Long(contractMainSubVO.getId()));
        }
        if(contractMainSubVO.getContractMainName()!=null) {
            contractGroupEntity.setGroupName(contractMainSubVO.getContractMainName());
        }
        ContractGroupEntity byIdE=null;
        if(contractMainSubVO.getId()!=null) {
            byIdE = getByIdE(new Long(contractMainSubVO.getId()));
        }
        Long contractGroupId=null;
        if(byIdE!=null){
            updateByIdE(contractGroupEntity);
        }else {
            contractGroupId=snowflakeIdWorker.nextId();
            contractGroupEntity.setId(contractGroupId);
            saveE(contractGroupEntity);
        }
        List<String> mainIds = contractMainSubVO.getContractMainIds();

        if(!CollectionUtils.isEmpty(mainIds)) {

            //先删除
            if(byIdE!=null){
                contractSubGroupDao.deleteContractSubGroup(new Long(contractMainSubVO.getId()));
            }
            for (String item : mainIds
                    ) {
                ContractSubGroupEntity csub = new ContractSubGroupEntity();
                if(contractMainSubVO.getId()!=null) {
                    Long aLong = new Long(contractMainSubVO.getId());
                    csub.setContractGroupId(aLong);

                }else {
                    csub.setContractGroupId(contractGroupId);
                }
                csub.setId(new Long(item));
                csub.setIsDelete(0);


                if(byIdE!=null){
                    csub.setId(new Long(item));
                    contractSubGroupService.saveE(csub);
                }else {
                    contractSubGroupService.saveE(csub);

                }
            }


        }
        return R.ok();
    }


    @Override
    public R updateByIdE(ContractGroupEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeOneById(Long id) {
        ContractGroupEntity entity = new ContractGroupEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);


        contractSubGroupDao.deleteContractSubGroup(id);


        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}
